<?php

/**
 * UserController
 * 
 * @ Konovalov Maxim aka ZloY	max.zloy@gmail.com
 * 
 */
class UserController extends Zend_Controller_Action {
	public function indexAction() {
	
	}
	
	public function registerAction()
	{
		$form = new Form_Register();
		$this->view->form = $form;
		if($this->getRequest()->isPost()){
			if($form->isValid($this->getRequest()->getPost()))
			{
				$user = new Model_User();
				$data = $this->getRequest()->getPost();
				$pass = $data['password'];
				$data['password'] = md5($pass);
				$user->fill($data);
				$user->save();
				$user->authenticate($data['login'], $data['password']);
				$this->_redirect('/');
			}
		}
	}
	
	public function loginAction()
	{
		$form = new Form_Register();
		$this->view->form = $form;
		if($this->getRequest()->isPost()){
			if($form->isValid($this->getRequest()->getPost())){
				$user = new Model_User();
				Zend_Debug::dump($form->getValue('password'));
				if($user->authenticate($form->getValue('login'), $form->getValue('password'))){
					$this->_redirect($_SERVER['HTTP_REFERER']);
				} else {
					$this->view->message = 'Не верный логин/пароль';
				}
			}
		}
	}
	
	public function logoutAction()
	{
		Zend_Auth::getInstance()->clearIdentity();
		$this->_redirect($_SERVER['HTTP_REFERER']);
	}
}